const app = getApp()
import BaseModel from '../../utils/common.js'
import goToNextPage from '../../utils/goToNextPage.js'
import checkAuth from '../../utils/checkAuth.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    phoneValue:"",//手机号
    verificationValue:"",//验证码
    time:"获取验证码",//获取验证码/60秒后重发
    currentTime:60,//60秒后重发
    disabled:false, //60秒内禁止再次点击
    phone_weixin:0,//手机号：0   还是微信：1
    backUrl:""
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      phone_weixin:wx.getStorageSync('phone_weixin') //获取
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      backUrl: decodeURIComponent(options.backUrl) || ''
    })
    checkAuth(this, false)
        .then(() => {
          // this.goToNextPage();
        })
        .catch(err => {})
  },
  /*监听手机号*/
  watchPhone:function(e){
    this.setData({
      phoneValue:e.detail.value
    })
  },
  /*监听验证码*/
  watchVerify:function(e){
    this.setData({
      verificationValue:e.detail.value
    })
  },
  /*获取验证码*/
  verification_obtain:function(){
    var that = this;
    if(!(/^1[3456789]\d{9}$/.test(that.data.phoneValue))){
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
      })
    }else{
      this.getCode(); //验证码倒计时
      this.getCodeData(); //获取验证码
    }
  },
  //获取验证码接口
  getCodeDataModel:new BaseModel({
    url: "sendCode",
    method:"get",
  }),
  getCodeData(res){
    this.getCodeDataModel
        .fetch({
          "phone":this.data.phoneValue
        })
        .then(res => {
          //失败
          wx.showToast({
            title: "验证码已发送，请注意查收",
            icon: 'none',
          })
        })
        .catch(res => {
          //失败
          wx.showToast({
            title: res.message,
            icon: 'none',
          })
        })
  },
  /*验证登录*/
  verify_login:function(){
    var that = this;
    if(!(/^1[3456789]\d{9}$/.test(that.data.phoneValue))){
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
      })
    }else if(!(/^[0-9]{6}$/.test(that.data.verificationValue))){
      wx.showToast({
        title: '验证码不正确',
        icon: 'none',
      })
    }else{
      this.getPhoneCodeData();
    }
  },
  //提交手机号，验证码接口
  getPhoneCodeDataModel:new BaseModel({
    url: "verification",
    method:"get",
  }),
  getPhoneCodeData(res){
    this.getPhoneCodeDataModel
        .fetch({
          "phone":this.data.phoneValue,//手机号
          "code":this.data.verificationValue //验证码
        })
        .then(res => {
          //成功
          wx.setStorageSync('phone_weixin', 1)
          this.setData({
            phone_weixin:1
          })
        })
        .catch(res => {
          //失败
          wx.showToast({
            title: res.message,
            icon: 'none',
          })
        })
  },
  //验证码倒计时函数
  getCode: function (options) {
    var interval;
    var that = this;
    var currentTime = that.data.currentTime;
    that.setData({
      time: currentTime + '秒后重发',
      disabled: true
    })
    interval = setInterval(function () {
      that.setData({
        time: (currentTime - 1) + '秒后重发',
        disabled: true
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          disabled: false
        })
      }
    }, 1000)
  },

  /*授权弹窗*/
  bindGetUserInfo:function(res){
    if (res.detail.userInfo) {
      //提交登录的用户的相关信息到数据库
      this.getUserMesCodeData(res);
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，某些功能将无法使用，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  //提交用户信息
  getUserMesCodeDataModel:new BaseModel({
    url: "employees/getUserinfo",
  }),
  getUserMesCodeData(res){
    this.getUserMesCodeDataModel
        .fetch({
          "encryptedData":res.detail.encryptedData,
          "iv":res.detail.iv,
          "rawData": res.detail.rawData
        })
        .then(res => {
          //成功
          wx.setStorageSync('authorize', 1) //授权 存到本地
          goToNextPage(this)
        })
        .catch(res => {
          //失败
          wx.showToast({
            title: '授权失败!',
            icon: 'none',
            duration: 2000
          })
        })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh(); //停止下拉
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.userName +'邀请你使用好望角教育员工端小程序，仅限好望角内部员工使用~',
      path: '/pages/login/login',
      imageUrl: "http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/share_ygImg.png",
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  },
})